package com.itbaizhan.rocketmqproducer.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//延迟消息消费者
/*延迟级别与时间会与理论不同，因为网络传输要时间*/
public class MyDelayMsgConsumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("producer_grp_01");
        consumer.setNamesrvAddr("192.168.66.100:9876");
        consumer.subscribe("topic_springboot_02","*");

        //消息监听器 实现MessageListenerConcurrently() 接口
        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //定义时间格式
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
                for (MessageExt msg:list) {
                    System.out.println(simpleDateFormat.format(new Date())+
                            "队列ID"+msg.getQueueId()+"\t"+msg.getMsgId()+
                            "延迟的级别"+msg.getDelayTimeLevel()+"\t"+
                            new String(msg.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }
}
